<?php

class cate extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    function _remap($method, $params = array()) {
        $this->_admin_remap($method, $params);
    }

    function index() {
        $this->load->model('Language');
        $data = array();
        foreach ($this->Language->get_all() as $row) {
            $data['langList'][$row['language_code']] = $row['language_name'];
        }
        $this->_setLayout('admin/cate/cate_list', 'admin_layout', $data);
    }

    function add() {
        $callback = $_REQUEST['callback'];
        $jsonOutPut = '';
        try {
            $data = json_decode($_GET['data'], true);
            if (empty($data)) {
                $jsonOutPut = json_encode(array('success' => false, 'message' => 'Can not save'));
            } else {
                $this->load->model('cate_model');
                if (isset($data['id'])) {
                    $this->cate_model->update($data, $data['id']);
                    $data_return = $this->cate_model->get_by_id($data['id']);
                    $data_return['edit'] = '<a href="' . base_url() . 'admin/cate/edit/' . $data['id'] . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>';
                    $jsonOutPut = json_encode(array('success' => true, 'message' => 'Saved successfully', 'data' => $data_return));
                } else {
                    $id = $this->cate_model->insert($data);
                    $data_return = $this->cate_model->get_by_id($id);
                    $data_return['edit'] = '<a href="' . base_url() . 'admin/cate/edit/' . $id . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>';
                    $jsonOutPut = json_encode(array('success' => true, 'message' => 'Loaded data', 'data' => $data_return));
                }
            }
        } catch (Exception $e) {
            $jsonOutPut = json_encode(array('success' => false, 'message' => 'Can not save'));
        }
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function delete() {
        $callback = $_REQUEST['callback'];
        $jsonOutPut = '';
        try {
            $data = json_decode($_GET['data'], true);
            $this->load->model('cate_model');
            $this->cate_model->delete($data);
            $jsonOutPut = json_encode(array('success' => true));
        } catch (Exception $e) {
            $jsonOutPut = json_encode(array('success' => false));
        }
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function grid() {
        $this->load->model('cate_model');
        $callback = $_REQUEST['callback'];
        $data = array();
        foreach ($this->cate_model->get_list($_GET['start'], $_GET['limit'], (isset($_GET['sort'])) ? $_GET['sort'] : 'id', (isset($_GET['dir'])) ? $_GET['dir'] : 'DESC') as $row) {
            $data[] = array_merge($row, array('edit' => '<a href="' . base_url() . 'admin/cate/edit/' . $row['id'] . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>'));
        }
        $jsonOutPut = json_encode(array('success' => true, 'message' => 'Loaded data', 'start' => $_GET['start'], 'total' => $this->cate_model->get_total(), 'data' => $data));
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function edit($id=0) {
        $this->load->model('cate_model');
        $this->load->model('Language');
        $this->load->library('helpfunc');
        $cattree = $this->cate_model->getCateSelectHtml();

        $data = array('datacontent' => (empty($id)) ? array() : $this->cate_model->get_by_id($id),
            'message' => null,'cattree'=>$cattree);
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $input_data = $_POST;
            if (!empty($id) && $this->cate_model->update($input_data, $id)) {
                header("Location: /admin/cate");
            } elseif ($id = $this->cate_model->insert($input_data, $id)) {
                header("Location: /admin/cate");
            }
            else
                $data['message'] = 'Update fail';
        }
        foreach ($this->Language->get_all() as $row) {
            $data['langList'][$row['language_code']] = $row['language_name'];
        }
        $this->_setLayout('admin/cate/cate_edit', 'admin_layout', $data);
    }

}

?>